package com.idata.common.constant;


import lombok.Data;

import java.io.Serializable;

/**
 * @File Result.Java
 * @Modify Time      @Author    @Version    @Description
 * ------------      -------    --------    -----------
 * 2023/4/27         yd.ji      1.0         None
 */
@Data
public class Result<T> implements Serializable {

    private int code;
    private String msg;
    private T data;
    private long timestamp;


    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS.code(), "操作成功", data);
    }

    public static <T> Result<T> error(Exception e) {
        return new Result<>(ResultCode.FAIL.code(), e.getMessage(), null);
    }

    public static <T> Result<T> build(ResultCode code, String msg, T data) {

        return new Result<>(code.code(), msg, data);
    }

    public static <T> Result<T> build(ResultCode code, String msg) {
        return build(code, msg, null);
    }

    public static <T> Result<T> build(ResultCode code) {
        return build(code, code.message(), null);
    }
}
